package com.houdoudou.affair.jsp;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houdoudou.bean.Machine;
import com.houdoudou.bean.Supplier;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.MachineDao;
import com.houdoudou.dao.SupplierDao;

public class ProgrammerAffairJsp2 {

/*------------------------------------------新增商家-------------------------------------------------*/	
	
	public static void createSupplier(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		 String jumpJspPath=req.getContextPath()+"/normalProgrammer/createSupplier.jsp"; 
		Supplier supplier=null;
		//获取Bean---------------------------
		try{
		 supplier= Supplier.beanFromRequest(req);
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
			    req.setAttribute("supplier", supplier);
			    Common.forwardToJsp(req,resp,"/normalProgrammer/createSupplier.jsp",errorInfo);
			    return ;
		 }
		
		//获取Bean---------------------------
				try{
				Supplier.checkValid(supplier);
				 supplier= Supplier.encrypt(supplier);
				 }catch(Exception e2){
					 e2.printStackTrace();
					 
					    success=false;
					    errorInfo="传入参数有误"+e2.getMessage();
					    req.setAttribute("supplier", supplier);
					   Common.forwardToJsp(req,resp,"/normalProgrammer/createSupplier.jsp",errorInfo);
					    return ;
				 }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   SupplierDao supplierDao= new  SupplierDao(conn);	  
		   Supplier supplierExist= supplierDao.selectById(supplier.getTelnum());
		   Supplier.print("after check exist in create supplier ", supplier);
		   if(!supplierExist.getTelnum().isEmpty()){
			   req.setAttribute("supplier", supplier);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/createSupplier.jsp","该电话已经注册");
		     }else{
		       supplierDao.create(supplier);
			   success=true;
			   errorInfo="创建成功";
			   req.setAttribute("supplier", supplier);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/createSupplier.jsp",errorInfo);}
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
			   req.setAttribute("supplier", supplier);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/createSupplier.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}
	
	private static  String  getSearchSql(Supplier supplier){
		String sql="select * from supplier";
		int count=0;
		if(! supplier.getTelnum().isEmpty()){ sql="select * from supplier  where telnum = "+supplier.getTelnum()+""; return sql;
		 }else{
			
			if(!supplier.getOwner().isEmpty()) {	
				if(count==0) {sql= sql+" where owner = "+supplier.getOwner();
				}else{
					sql= sql+" and  owner = "+supplier.getOwner();
				  }  
				  count++; }
			
		}
		return sql;
	}
	
/*------------------------------------------搜索商家-------------------------------------------------*/	
	
	public static void searchSupplier(HttpServletRequest req, HttpServletResponse resp){		
		
		String  errorInfo=null;
		//获取Bean---------------------------		
		Supplier supplier;
		try {
			supplier = Supplier.beanFromRequest(req);
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			 Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp","传入参数有误"+e2.getMessage());
			 return;
		}
	//	supplier.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Supplier> suppliers = new ArrayList<Supplier>();
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();  
		   SupplierDao supplierDao= new  SupplierDao(conn);
		   String sql=getSearchSql(supplier);
		   System.out.println(sql);
		   suppliers= supplierDao.search(sql);	  		   
		   req.setAttribute("success", true);	
  		   req.getSession().setAttribute("suppliers", suppliers);	
           Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp",null);
		}catch(Exception e){
			e.printStackTrace();
			  errorInfo="数据库异常";
			  req.setAttribute("success", false);		
	          Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
		  
			  
					
			 
			 
	}
/*------------------------------------------修改商家-------------------------------------------------*/	
	
	public static void modifySupplier(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo=null;
		Supplier supplier = null;
		//获取Bean---------------------------
		try{
		 supplier= Supplier.beanFromRequest(req);
		 if(supplier.getTelnum().isEmpty()) { errorInfo="请传入参数 :商家电话";
		    Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",errorInfo);
		    return;}
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
			    Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",errorInfo);
			    return ;
		 }
	
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		  
		   SupplierDao supplierDao= new  SupplierDao(conn);	  
			Supplier supplierExist=  supplierDao.selectById(supplier.getTelnum());
			if(supplierExist.getTelnum().isEmpty()){
				 req.setAttribute("supplier", supplier);
				 Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp","要修改的商家不存在");
				 return;
				
			}else{
			   success=true;
			   req.setAttribute("success", true);
			   req.setAttribute("supplier", supplierExist);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",null);
			   return;
			}
		}catch(Exception e){
			e.printStackTrace();
			
			 success=false;
			   errorInfo="数据库异常";
		    Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}

/*------------------------------------------更新商家-------------------------------------------------*/	
	
	public static void updateSupplier(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo=null;
		Supplier supplier = null;
		//获取Bean---------------------------
		try{
		 supplier= Supplier.beanFromRequest(req);		
		 }catch(Exception e2){
			 e2.printStackTrace();		 
			    success=false;
			    errorInfo="传入参数有误"+e2.getMessage();
			    Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",errorInfo);
			    return ;
		 }		
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		Supplier supplierExist = null;
		try{
		   conn= ConnFactory.getConn();
		 
		    SupplierDao supplierDao= new  SupplierDao(conn);	  
			supplierExist=  supplierDao.selectById(supplier.getTelnum());
			supplierExist= Supplier.decrypt(supplierExist);
			supplier= Supplier.merge(supplierExist, supplier);		
			try{Supplier.checkValid(supplier);
			 supplier= Supplier.encrypt(supplier);}catch(Exception e){
				req.setAttribute("supplier", supplier);
				 Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",e.getMessage());
			     return;	 
			}
		    conn.setAutoCommit(false);
		   
			supplierDao.update(supplier);
		    conn.commit();
			   success=true;
			   req.setAttribute("success", true);
			   req.setAttribute("supplier", supplier);
			   Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp","修改成功");
		  
		}catch(Exception e){
			e.printStackTrace(); 
			try { conn.rollback();		} catch (SQLException e1) {	e1.printStackTrace();	}
			  success=false;
			   errorInfo="数据库异常";
			   req.setAttribute("supplier", supplier);
		    Common.forwardToJsp(req,resp,"/normalProgrammer/modifySupplier.jsp",errorInfo);
		}finally{
			 ConnFactory.closeConn(conn);	
				
		}
				 
	}
/*------------------------------------------删除商家-------------------------------------------------*/	
	
	public static void deleteSupplier(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		 Supplier supplier = null;
		try {
			supplier = Supplier.beanFromRequest(req);
		} catch (Exception e2) {
			e2.printStackTrace();
			 Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp","传入数据异常"+e2.getMessage());
			 return;
		}
		// 检验数据合法
		 if(supplier.getTelnum().length()!=11){
			 Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp","传入商家电话位数不对");
			 return;
		 }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   SupplierDao supplierDao= new SupplierDao(conn);
		   supplierDao.delete(supplier);
		   success=true;
		  List<Supplier> suppliers= (List<Supplier>) req.getSession().getAttribute("suppliers");
		  for(int i=suppliers.size()-1;i>=0;i--){
			
			  if(suppliers.get(i).getTelnum().equals(supplier.getTelnum())) {  suppliers.remove(i); break;}
		  }
		  req.getSession().setAttribute("suppliers", suppliers);
		  req.setAttribute("success", true);
		   errorInfo="删除商家"+supplier.getTelnum()+"成功";
		   Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp",errorInfo);
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try { conn.rollback();	} catch (SQLException e1) {	e1.printStackTrace();}
			 success=false;
			 Common.forwardToJsp(req,resp,"/normalProgrammer/manageSupplier.jsp","数据库异常");
		}finally{
			 ConnFactory.closeConn(conn);	
		}
				
				
			 
	}
	
	
}
